package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.itheim.client.pojo.Dish;
import com.itheim.client.vo.DishVo;
import com.itheim.client.vo.FlavorsVo;
import com.itheim.client.vo.SetmealDishesVo;
import com.itheim.client.vo.SetmealVo;
import com.itheima.client.mapper.CDishMapper;
import com.itheima.client.mapper.CFlavorsMapper;
import com.itheima.client.mapper.CSetmealMapper;
import com.itheima.common.R;
import com.itheima.service.CDishService;
import com.itheima.service.CSetmealService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author longbosheng
 * @Date 2022/6/20 9:17
 * @PackageName:com.itheima.service
 * @ClassName: SetmealService
 * @Description: TODO
 * @Version 1.0
 */
@Log4j2
@Service
public class CSetmealServiceImpl implements CSetmealService {
    @Autowired
    private CSetmealMapper csetmealMapper;
    @Autowired
    private CDishMapper cdishMapper;
    @Autowired
    private CFlavorsMapper cflavorsMapper;
    @Autowired
    private CDishService dishService;
    /**
     * 套餐列表
     *
     * @param categoryId
     * @param status
     * @return
     */
    @Override
    public R showList(String categoryId, int status) {
        QueryWrapper<SetmealVo> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", categoryId);
        wrapper.eq("status", status);
        List<SetmealVo> setmealVos = csetmealMapper.selectList(wrapper);
        if (setmealVos.size()==0){
            List<Dish> dishes = dishService.dishList(categoryId, status);
            if (CollectionUtils.isEmpty(dishes)){
                return R.error("该分类没有任何菜品");
            }
            log.info("查到的是菜品id");
            return R.success(dishes);
//            return R.success("查到的是菜品id");
        }
        return R.success(setmealVos);
    }

    /**
     * 菜品详情
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R dishInfo(String id) {
        /*按id查询菜品信息*/
        DishVo dishVo = cdishMapper.selectDishById(id);
        if (dishVo==null){
            List<SetmealDishesVo> setmealDishesVos = csetmealMapper.selectSetmealById(id);
            return setmealDishesVos.size()>0 ? R.success(setmealDishesVos) : R.error("菜品id异常，找不到此菜品");
        }
        /*增加口味表和对应的菜品分类*/
        List<FlavorsVo> flavorsVos = cflavorsMapper.selectFlavorsById(id);
        dishVo.setFlavors(flavorsVos);
        return R.success(dishVo);
    }
}
